package com.lixiaoyi.config.security.filter;

import com.lixiaoyi.config.exception.CustomerAuthenticationException;
import com.lixiaoyi.config.redis.RedisService;
import com.lixiaoyi.config.security.handle.LoginFailureHandler;
import com.lixiaoyi.config.security.service.CustomerUserDetailsService;
import com.lixiaoyi.utils.JwtUtils;
import io.netty.util.internal.ObjectUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.Resource;
import org.springframework.security.core.AuthenticationException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.RequestWrapper;
import java.io.IOException;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-07-01
 */

@Data
@Component
public class CheckTokenFilter extends OncePerRequestFilter {


    @Resource
    private LoginFailureHandler loginFailureHandler;

    @Resource
    private RedisService redisService;

    @Resource
    private JwtUtils jwtUtils;

    @Resource
    private CustomerUserDetailsService customerUserDetailsService;



    @Value("${request.login.url}")
    private String loginUrl;

    /**
     * 验证token
     * @param request
     * @param response
     * @param filterChain
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        try {
            // 获取当前请求的url地址   注意：getRequestURI
            String url = request.getRequestURI();
            // 如果当前的请求不是登录请求，则需要进行token验证
            if (!url.equals(loginUrl)){
                this.validateToken(request);
            }
        }catch (AuthenticationException e){
            loginFailureHandler.onAuthenticationFailure(request,response,e);
        }

        // 登录请求不需要验证token
        doFilter(request,response,filterChain);

    }

    private void validateToken(HttpServletRequest request) {

        // 获取前端提交token信息
        // 从headers头部获取token信息
        String token = request.getHeader("token");
        // 如果请求头部没有获取token，则从请求的参数中进行获取
        if (ObjectUtils.isEmpty(token)){
            token = request.getParameter("token"); // 从参数中获取
        }
        // 如果请求参数也不存在token信息 则抛出异常
        if (ObjectUtils.isEmpty(token)){
            throw new CustomerAuthenticationException("token不存在");
        }
        // 判断redis中是否存在该token
        String tokenKey = "token_" + token;
        String redisToken = redisService.get(tokenKey);

        // 判断redis是否存在token信息 若为空 咋表示token已经失效
        if (ObjectUtils.isEmpty(redisToken)){
            throw new CustomerAuthenticationException("token已过期");
        }
        // 如果token和Redis中的token不一致，则验证失败
        if (!token.equals(redisToken)){
            throw new CustomerAuthenticationException("token验证失败");
        }

        // 如果存在token ，则从token中解析出用户名
        String username = jwtUtils.getUsernameFromToken(token);
        // 如果用户名为空 则解析失败
        if (ObjectUtils.isEmpty(username)){
            throw new CustomerAuthenticationException("token解析失败");
        }
        // 获取用户信息
        UserDetails userDetails = customerUserDetailsService.loadUserByUsername(username);
        // 判断用户信息是否为空
        if (userDetails == null){
            throw new CustomerAuthenticationException("token验证失败");
        }
        // 创建身份验证对象
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(userDetails, null,
                        userDetails.getAuthorities());
        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));

        // 将验证信息设置到Spring Security上下文
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);

    }
}
